package com.whately.core.base.fileupload.qiniu.conf;

import com.whately.core.base.fileupload.qiniu.constants.QiNiuZoneCommonBucketEnum;
import com.whately.core.base.fileupload.qiniu.constants.QiNiuZoneHtmlBucketEnum;
import com.whately.core.base.fileupload.qiniu.constants.QiNiuZonePrivateBucketEnum;
import com.whately.core.base.fileupload.qiniu.model.BucketModel;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Component
@ConfigurationProperties(prefix = "myconfig.uploadfile")
@Slf4j
public class QiNiuSelfDefineConfigUploadFile {

    @Getter
    @Setter
    private boolean httpsOpenForQiNiu=false;

    @Getter
    @Setter
    private Map<String, BucketModel> bucketMap = new HashMap<>();

    @PostConstruct
    public void init(){
        log.info("httpsOpenForQiNiu {} ,bucket map size is {} is {}",httpsOpenForQiNiu,bucketMap.size(),bucketMap);
        if(bucketMap.containsKey(QiNiuZonePrivateBucketEnum.BUCKET_CORP_CERT.getBucketCode())
        ||bucketMap.containsKey(QiNiuZonePrivateBucketEnum.PRIVATE_IMG.getBucketCode())
        ||bucketMap.containsKey(QiNiuZoneCommonBucketEnum.PUB_IMG_COMMON.getBucketCode())
                ||bucketMap.containsKey(QiNiuZoneHtmlBucketEnum.HTML_FILE.getBucketCode())
                ){
            log.error("BucketConfiguration not allowed bucket {}",bucketMap.keySet());
            throw new RuntimeException("BucketConfiguration not allowed bucket");
        }

        //把图片bucket的加进去
        BucketModel model = new BucketModel();
        model.setText(QiNiuZonePrivateBucketEnum.BUCKET_CORP_CERT.getText());
        model.setPrivateZone(true);
        model.setCutFlag(true);
        model.setWidth(800);
        model.setHeight(600);
        bucketMap.put(QiNiuZonePrivateBucketEnum.BUCKET_CORP_CERT.getBucketCode(),model);

        model = new BucketModel();
        model.setText(QiNiuZonePrivateBucketEnum.PRIVATE_IMG.getText());
        model.setPrivateZone(true);
        model.setCutFlag(false);
        bucketMap.put(QiNiuZonePrivateBucketEnum.PRIVATE_IMG.getBucketCode(),model);

        model = new BucketModel();
        model.setText(QiNiuZoneCommonBucketEnum.PUB_IMG_COMMON.getText());
        model.setPrivateZone(false);
        model.setCutFlag(false);
        bucketMap.put(QiNiuZoneCommonBucketEnum.PUB_IMG_COMMON.getBucketCode(),model);

        log.info("full bucket map size is {} is {}",bucketMap.size(),bucketMap);
    }
}
